package top.blacknota.web.controller;

import act.Act;
import cn.hutool.db.DbUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.nutz.dao.Dao;
import org.nutz.dao.impl.NutDao;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import top.blacknota.pojo.User;
import top.blacknota.service.UserService;
import top.blacknota.utils.exception.LpException;

import java.util.Date;
import java.util.List;

/**
 * Created by 060016 on 2017/5/22.
 */
public class UserController extends Global{

    UserService userService = new UserService();

    @GetAction("/user/login/form")
    public void form() {
    }

    @GetAction("/r")
    public void init(){
        Dao dao = new NutDao(DbUtil.getDs("zc"));
        if (dao.count(User.class) == 0) {
            User user = new User();
            Act.app().classLoader();

            user.setUsername("admin");
            user.setPassword("123");
            user.setCreateTime(new Date());
            dao.insert(user);
        }
    }

    @PostAction("/user/login")
    public JSONObject login(String username, String password) {
        JSONObject result = new JSONObject();
        String me = session.get("me");
        if (me!=null){
            result.put("status","200");
            return result;
        }
        try {
            User user = userService.login(username, password);
            session.put("name",user.getId());
            result.put("status","200");
        } catch (LpException e) {
            result.put("status","400");
            result.put("msg",e.getMessage());
        }
        return result;
    }

    @GetAction("/user/logout")
    public void logout() {
        session.clear();
        redirect("/user/login/form");
    }
    @GetAction("/user/alluser")
    public JSONArray allUser() {
        JSONArray array = null;
        try {
            List<User> users = userService.allUsers();
            array = new JSONArray(users);
        } catch (LpException e) {
            e.printStackTrace();
        }
        return  array;
    }

    @GetAction("/user/add/form")
    public void addUser() {
    }
    @GetAction("/user/edit/{userId}")
    public void editUser(String userId) {
        User user = userService.findUserById(userId);
        render(user);
    }
    @PostAction("/user/edit")
    public JSONObject edit(String json) {
        User user = JSONUtil.toBean(JSONUtil.parseObj(json), User.class);
        JSONObject result = new JSONObject();
        try{
            userService.editUser(user);
            result.put("status",200);
            result.put("msg","修改成功");
        }catch (LpException e){
            result.put("status",500);
            result.put("msg",e.getMessage());
        }
        return result;
    }

    @PostAction("/user/add")
    public JSONObject add(String json) {
        User user = JSONUtil.toBean(JSONUtil.parseObj(json), User.class);
        System.out.println(user);
        JSONObject result = new JSONObject();
        try{
            userService.addUser(user);
            result.put("status",200);
            result.put("msg","添加成功");
        }catch (LpException e){
            result.put("status",500);
            result.put("msg",e.getMessage());
        }
        return result;
    }

    //删除用户
    @PostAction("/user/deleteUser/{userId}")
    public JSONObject delete(String userId){
        JSONObject result = new JSONObject();
        try{
            userService.deleteUser(userId);
            result.put("status",200);
            result.put("msg","删除成功");
        }catch (LpException e){
            result.put("status",500);
            result.put("msg",e.getMessage());
        }
        return result;
    }


}
